


let curUnionID = null;

function fixData(dataArr) {

    let arr = [];
    for (let i = 0; i < dataArr.length; ++i) {
        let newData = {};
        let data = dataArr[i];
        for (let key in data) {
            if (key === 'unionInfo') continue;
            if (data.hasOwnProperty(key)) {
                newData[key] = data[key];
            }
        }
        let unionInfo = data.unionInfo.find(function (e) {
            return e.unionID == curUnionID;
        });
        if (!!unionInfo) {
            for (let key in unionInfo) {
                if (unionInfo.hasOwnProperty(key)) {
                    newData[key] = unionInfo[key];
                }
            }
        }
        arr.push(newData);
    }
    return arr;
}

function createList(pageNo = 1, pageSize = 10) {

    let matchData = {};
    let uid = $("#txtSearch").val();
    if (!!uid) {
        matchData.uid = parseInt(uid || "");
    }
    matchData["unionID"] = curUnionID;
    var start = (pageNo - 1) * pageSize;
    apiAdminGetRecord("unionPayModel", start, pageSize, matchData, function (data) {
        if (data.code === 0) {
            if (data.code === 0) {
                setTimeout(() => {
                    $('#dataList').bootstrapTable('load', data.msg);
                }, 200)
            }
        }
    });
}


$(document).ready(function () {
    // 初始化数据列名

    var parameters = parseQueryString(window.location.href);
    curUnionID = parameters.unionID;

    //数据表格初始化
    $('#dataList').bootstrapTable({
        showRefresh: true,
        striped: true, // 是否显示行间隔色
        pageNumber: 1, // 初始化加载第一页
        pagination: true, // 是否分页
        sidePagination: 'server', // server:服务器端分页|client：前端分页
        pageSize: 10, // 单页记录数
        pageList: [10, 20, 30],
        theadClasses: "thead-light",//这里设置表头样式
        totalField: "totalCount",
        smartDisplay: false,
        dataField: "recordArr",
        maintainSelected: true,
        buttonsClass: 'light',
        buttonsPrefix: 'btn',
        showRefresh: false,// 刷新按钮
        onPageChange: function (number, size) {
            createList(number, size);
        },
        queryParams: function (params) { // 上传服务器的参数
            var temp = {//如果是在服务器端实现分页，limit、offset这两个参数是必须的
                limit: params.limit, // 每页显示数量
                offset: params.offset, // SQL语句起始索引
            };
            return temp;
        },
        onPostBody: function () {
            //重新启用弹出层,否则formatter格式化函数返回的html字符串上的tooltip提示不生效
            $('[data-bs-toggle="tooltip"]').each(function (i, el) {
                new bootstrap.Tooltip(el)
            })
        },
        onLoadSuccess: function () {
            //重新启用弹出层,否则formatter格式化函数返回的html字符串上的tooltip提示不生效
            $('[data-bs-toggle="tooltip"]').each(function (i, el) {
                new bootstrap.Tooltip(el)
            })
        },
        queryParams: function (params) { // 上传服务器的参数
            var temp = {//如果是在服务器端实现分页，limit、offset这两个参数是必须的
                limit: params.limit, // 每页显示数量
                offset: params.offset, // SQL语句起始索引
            };
            return temp;
        },
        columns: [{
            title: 'id',
            field: '_id',
            visible: false
        },
        {
            title: '用户ID',
            width: 100,
            align: "center",
            valign: "middle",
            visible: false,
            field: 'uid',
        }, {
            title: '昵称',
            width: 100,
            align: "center",
            valign: "middle",
            visible: false,
            field: 'nickname',
        },
        {
            title: '头像',
            width: 100,
            align: "center",
            valign: "middle",
            field: 'avatar',
            visible: false,
            formatter: avaterImg
        },
        {
            title: '联盟ID',
            width: 100,
            align: "center",
            valign: "middle",
            field: 'unionID',
        },
        {
            title: '年份',
            width: 100,
            align: "center",
            valign: "middle",
            field: 'year',
        },
        {
            title: '月份',
            width: 100,
            align: "center",
            valign: "middle",
            field: 'month',
        },
        {
            title: '数量',
            width: 150,
            align: "center",
            valign: "middle",
            field: 'count',
        },
        {
            title: '周期',
            width: 150,
            align: "center",
            valign: "middle",
            field: 'day'
        },
        {
            title: '操作',
            width: 100,
            align: "center",
            visible: false,
            valign: "middle",
            formatter: option
        }]
    });
    createList(1);
    $('#dataList').bootstrapTable('hideLoading');
});

function dateMatter(value, row, index) {
    return new Date(value).toLocaleString();
}

function scoreMatter(value, row, index) {
    return value ? value.toFixed(2) : 0;
}


// 定义删除、更新按钮
function option(value, row, index) {
    var htm = "";
    let curDay = row.day.trim().toString();
    htm += `  <button type="button" class="btn btn-light"  data-bs-toggle="tooltip" data-bs-placement="top" data-bs-title="查询消耗明细" onClick="unionScore(${row.uid},'${curDay}')"><i class="bi bi-receipt fs-5 text-success"></i></button>`;
    // htm += `  <button type="button" class="btn btn-light"  data-bs-toggle="tooltip" data-bs-placement="top" data-bs-title="查看游戏记录" onClick="unionScore(${row.uid})"><i class="bi bi-controller fs-5 text-success"></i></button>`;
    return htm;
}


function unionScore(uid, curDay) {

    window.rolemodal = $.modal({
        url: `unionPayChange.html?unionID=${curUnionID}&uid=${uid}&day=${curDay}`,
        title: '消耗明细',
        buttons: [],
        modalDialogClass: 'modal-dialog-centered modal-xl',
    })
}

function avaterImg(value, row, index) {
    var htm = "";
    htm = `<img  style="width: 40px ; height: 40px;"  src="${value}" class="img-thumbnail" >`;
    return htm;
}